#ifndef __PROXY_HELPER__
#define __PROXY_HELPER__

#include "definitions.h"

// Global variables
pthread_mutex_t  g_mutex;
pthread_mutex_t  g_sslmutex;
int              g_workerthreadcount;
SSL_CTX*         g_psslcontext;
pthread_mutex_t* g_lockarray;

// Function pointers for incoming and outgoing data (will be handled by incoming/outgoing SOs
char* (*g_phandle_incoming)(char* bytes, int* length);
char* (*g_phandle_outgoing)(char* bytes, int* length);

struct CRYPTO_dynlock_value
{
	pthread_mutex_t mutex;
};

// Set Socket Timeout Values
void setsockettimeout(int sock, int timeout);

// Connect to the server and get a valid socket handle
int connectserver();

// Init SSL Server
SSL_CTX* initserverctx(void);

// Load the Certs
void loadcertificates(SSL_CTX* ctx);

// Send the Source Info
void sendsourceinfo(int clientfd, int serverfd);

// Log SSL Errors
void logsslerror(char* task, SSL* ssl, int code);

// Increment The Client Count
void incrementworkercount();

// Decrement the Client count
void decrementworkercount();

// Validates the client count
int validateclientcount();

// Drain the socket buffer
int drainsocketbuffer(int fd, SSL* ssl);

// setup ssl context
SSL* setupssl(int clientfd, SSL_CTX* ctx);

/**** OPENSSL setup for multi-threaded application ****/
void lock_callback(int mode, int type, char *file, int line);
unsigned long thread_id(void);
void init_locks(void); 
void kill_locks(void);
struct CRYPTO_dynlock_value* dyn_create_func(const char* file, int line);
void dyn_destroy_func(struct CRYPTO_dynlock_value* l, const char* file, int line);
void dyn_lock_func(int mode, struct CRYPTO_dynlock_value* l, const char* file, int line);

#endif // __PROXY_HELPER__